package com.purong.purong.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.TimePickerView;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.bean.CarBrandBean;
import com.purong.purong.bean.CarBrandListBean;
import com.purong.purong.bean.CarConfigBean;
import com.purong.purong.bean.CarConfigListBean;
import com.purong.purong.bean.CarModelBean;
import com.purong.purong.bean.CarModelListBean;
import com.purong.purong.bean.TextItemBean;
import com.purong.purong.bean.UserCarBean;
import com.purong.purong.toggle.ToggleButton;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.util.NumDecTextWatch;
import com.purong.purong.util.ZeroStartUnableTextWatch;
import com.purong.purong.widget.TextChoseDialog;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zengwenjie on 2017/12/15.
 */
public class UserCarAddActivity extends BaseActivity {
    private EditText moneyEd,worthEd,ageEd,loanEd,repayEd,restEd;
    private TextView brandTv,modelTv,configTv,buyTimeTv,repayTimeTv;
    private Button confirm;
    private TextChoseDialog mBrandDialog,mModelDialog,mConfigDialog;
    private CarConfigBean configBean;
    private CarBrandBean brandBean;
    private CarModelBean modelBean;
    private List<TextItemBean> brandList=new ArrayList<>();
    private List<TextItemBean> modelList=new ArrayList<>();
    private List<TextItemBean> configList=new ArrayList<>();

    private UserCarBean carBean;
    private boolean isTurnOn;
    private ToggleButton statusBtn;
    @Override
    public void initBundle() {
        carBean =(UserCarBean)getIntent().getSerializableExtra("USER_CAR");
    }

    @Override
    public int getRootViewId() {
        return R.layout.activity_user_car_add;
    }

    @Override
    public void initView() {
        showBack();
        showTitle("车辆信息");
        statusBtn = (ToggleButton) findViewById(R.id.status_btn);


        statusBtn.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on) {
                    isTurnOn=true;
                    findViewById(R.id.more_lay).setVisibility(View.VISIBLE);
                } else {
                    isTurnOn=false;
                    findViewById(R.id.more_lay).setVisibility(View.GONE);
                    loanEd.setText("");
                    repayEd.setText("");
                    restEd.setText("");
                    repayTimeTv.setText("请选择");
                    repayTimeTv.setTextColor(getResources().getColor(R.color.text_gray));
                }
            }
        });
        brandTv=(TextView)findViewById(R.id.car_brand_tv);
        modelTv=(TextView)findViewById(R.id.car_model_tv);
        configTv=(TextView)findViewById(R.id.car_config_tv);
        buyTimeTv=(TextView)findViewById(R.id.buy_time_tv);
        repayTimeTv=(TextView)findViewById(R.id.repay_time_tv);

        buyTimeTv=(TextView)findViewById(R.id.buy_time_tv);
        moneyEd=(EditText)findViewById(R.id.car_money_ed);
        worthEd=(EditText)findViewById(R.id.car_worth_ed);
        ageEd=(EditText)findViewById(R.id.car_age_ed);
        loanEd=(EditText)findViewById(R.id.loan_money_ed);
        repayEd=(EditText)findViewById(R.id.repay_count_ed);
        restEd=(EditText)findViewById(R.id.rest_money_ed);
        confirm=(Button)findViewById(R.id.confirm);
        moneyEd.addTextChangedListener(new NumDecTextWatch(moneyEd));
        worthEd.addTextChangedListener(new NumDecTextWatch(worthEd));
        ageEd.addTextChangedListener(new ZeroStartUnableTextWatch(ageEd));
        loanEd.addTextChangedListener(new NumDecTextWatch(loanEd));
        repayEd.addTextChangedListener(new ZeroStartUnableTextWatch(repayEd));
        restEd.addTextChangedListener(new NumDecTextWatch(restEd));
        if (carBean ==null){
            confirm.setText("保存，进入下一车辆");
        }else {
            confirm.setText("保存");
        }

    }
    private void setData(){
        if (carBean ==null)
            return;
        brandBean=new CarBrandBean();
        brandBean.setCar_brand_id(carBean.getCar_brand_id());
        brandBean.setCar_brand_name(carBean.getCar_brand_name());
        brandTv.setText(brandBean.getCar_brand_name());
        brandTv.setTextColor(getResources().getColor(R.color.blue));
        modelBean=new CarModelBean();
        modelBean.setCar_model_id(carBean.getCar_model_id());
        modelBean.setCar_model_name(carBean.getCar_model_name());
        modelTv.setText(modelBean.getCar_model_name());
        modelTv.setTextColor(getResources().getColor(R.color.blue));
        configBean=new CarConfigBean();
        configBean.setCar_config_id(carBean.getCar_config_id());
        configBean.setCar_config_name(carBean.getCar_config_name());
        configTv.setText(configBean.getCar_config_name());
        configTv.setTextColor(getResources().getColor(R.color.blue));
        buyTimeTv.setText(carBean.getBuy_time());
        if (carBean.getBuy_time()!=null){
            buyTimeTv.setText(carBean.getBuy_time());
            buyTimeTv.setTextColor(getResources().getColor(R.color.blue));
        }
        if (carBean.getCar_money()!=null){
            moneyEd.setText(carBean.getCar_money());
        }
        if (carBean.getWorth()!=null){
            worthEd.setText(carBean.getWorth());
        }
        if (carBean.getCar_age()!=null){
            ageEd.setText(carBean.getCar_age());
        }

        if (carBean.getLoan_money()!=null&&!carBean.getLoan_money().equals("")){
            loanEd.setText(carBean.getLoan_money());
            isTurnOn=true;
        }
        if (carBean.getRepay_count()!=null&&!carBean.getRepay_count().equals("")){
            repayEd.setText(carBean.getRepay_count());
            isTurnOn=true;
        }
        if (carBean.getRest_money()!=null&&!carBean.getRest_money().equals("")){
            restEd.setText(carBean.getRest_money());
            isTurnOn=true;
        }
        if (carBean.getRepay_time()!=null&&!carBean.getRepay_time().equals("")){
            repayTimeTv.setText(carBean.getRepay_time());
            repayTimeTv.setTextColor(getResources().getColor(R.color.blue));
            isTurnOn=true;
        }
        if (isTurnOn){
            findViewById(R.id.more_lay).setVisibility(View.VISIBLE);
            statusBtn.toggleOn();
        }else {
            findViewById(R.id.more_lay).setVisibility(View.GONE);
            statusBtn.toggleOff();
        }
    }



    private void addCar(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (brandBean==null){
            showToast("车辆品牌为必选项");
            return;
        }else {
            params.put("car_brand_id",brandBean.getCar_brand_id());
        }
        if (modelBean==null){
            showToast("车辆型号为必选项");
            return;
        }else {
            params.put("car_model_id",modelBean.getCar_model_id());
        }
        if (configBean==null){
            showToast("车辆配置为必选项");
            return;
        }else {
            params.put("car_config_id",configBean.getCar_config_id());
        }
        if (buyTimeTv.getText().equals("请选择")){
            showToast("购买时间为必填项");
            return;
        }else {
            params.put("buy_time",buyTimeTv.getText().toString().trim());
        }
        if (moneyEd.getText().toString().trim().equals("")){
            showToast("购买价格为必填项");
            return;
        }else {
            params.put("car_money",moneyEd.getText().toString().trim());
        }
        params.put("worth",worthEd.getText().toString().trim());
        if (ageEd.getText().toString().trim().equals("")){
            showToast("车龄为必填项");
            return;
        }else {
            params.put("car_age",ageEd.getText().toString().trim());
        }
        if (isTurnOn){
            params.put("loan_money",loanEd.getText().toString().trim());
            params.put("repay_count",repayEd.getText().toString().trim());
            params.put("rest_money",restEd.getText().toString().trim());
            if (!repayTimeTv.getText().toString().trim().equals("请选择")){
                params.put("repay_time",repayTimeTv.getText().toString().trim());
            }else {
                params.put("repay_time","");

            }
        }else {
            params.put("loan_money","");
            params.put("repay_count","");
            params.put("rest_money","");
            params.put("repay_time","");
        }

        request(Contant.CAR_ADD,params,true,true);
    }
    private void editCar(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("user_car_id",carBean.getUser_car_id());
        if (brandBean==null){
            showToast("车辆品牌为必选项");
            return;
        }else {
            params.put("car_brand_id",brandBean.getCar_brand_id());
        }
        if (modelBean==null){
            showToast("车辆型号为必选项");
            return;
        }else {
            params.put("car_model_id",modelBean.getCar_model_id());
        }
        if (configBean==null){
            showToast("车辆配置为必选项");
            return;
        }else {
            params.put("car_config_id",configBean.getCar_config_id());
        }
        if (buyTimeTv.getText().equals("请选择")){
            showToast("购买时间为必选项");
            return;
        }else {
            params.put("buy_time",buyTimeTv.getText().toString().trim());
        }
        if (moneyEd.getText().toString().trim().equals("")){
            showToast("购买价格为必填项");
            return;
        }else {
            params.put("car_money",moneyEd.getText().toString().trim());
        }
        params.put("worth",worthEd.getText().toString().trim());
        if (ageEd.getText().toString().trim().equals("")){
            showToast("车龄为必填项");
            return;
        }else {
            params.put("car_age",ageEd.getText().toString().trim());
        }
        if (isTurnOn){
            params.put("loan_money",loanEd.getText().toString().trim());
            params.put("repay_count",repayEd.getText().toString().trim());
            params.put("rest_money",restEd.getText().toString().trim());
            if (!repayTimeTv.getText().toString().trim().equals("请选择")){
                params.put("repay_time",repayTimeTv.getText().toString().trim());
            }else {
                params.put("repay_time","");

            }
        }else {
            params.put("loan_money","");
            params.put("repay_count","");
            params.put("rest_money","");
            params.put("repay_time","");
        }
        request(Contant.CAR_EDIT,params,true,true);
    }

//    private void initBrandDialog(){
//        mBrandDialog=new TextChoseDialog(this);
//        mBrandDialog.setOnTextChoseListener(new TextChoseDialog.OnTextChoseListener() {
//            @Override
//            public void onChose(int position, TextItemBean item) {
//                brandBean=item;
//                brandTv.setText(brandBean.getName());
//                brandTv.setTextColor(getResources().getColor(R.color.blue));
//                modelBean=null;
//                modelTv.setText("请选择");
//                modelTv.setTextColor(getResources().getColor(R.color.text_gray));
//                configBean=null;
//                configTv.setText("请选择");
//                configTv.setTextColor(getResources().getColor(R.color.text_gray));
//            }
//
//        });
//
//        mBrandDialog.setData(brandList);
//    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.CAR_ADD)){
            if (responseInfo.isRequestOk){
                showToast("添加成功");
                Intent intent=new Intent(this,UserCarAddActivity.class);
                startActivity(intent);
                this.finish();
            }
        }else if (responseInfo.url.equals(Contant.CAR_EDIT)){
            if (responseInfo.isRequestOk){
                showToast("编辑成功");
                this.finish();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1){
            if (resultCode==RESULT_OK){
                brandBean=(CarBrandBean)data.getSerializableExtra("CAR_BRAND");
                if (brandBean!=null){
                    brandTv.setText(brandBean.getCar_brand_name());
                    brandTv.setTextColor(getResources().getColor(R.color.blue));
                    modelBean=null;
                    modelTv.setText("请选择");
                    modelTv.setTextColor(getResources().getColor(R.color.text_gray));
                    configBean=null;
                    configTv.setText("请选择");
                    configTv.setTextColor(getResources().getColor(R.color.text_gray));
                }
            }
        }else if (requestCode==2){
            if (resultCode==RESULT_OK){
                modelBean=(CarModelBean)data.getSerializableExtra("CAR_MODEL");
                if (modelBean!=null){
                    modelTv.setText(modelBean.getCar_model_name());
                    modelTv.setTextColor(getResources().getColor(R.color.blue));
                    configBean=null;
                    configTv.setText("请选择");
                    configTv.setTextColor(getResources().getColor(R.color.text_gray));
                }
            }
        }else if (requestCode==3){
            if (resultCode==RESULT_OK){
                configBean=(CarConfigBean) data.getSerializableExtra("CAR_CONFIG");
                if (configBean!=null){
                    configTv.setText(configBean.getCar_config_name());
                    configTv.setTextColor(getResources().getColor(R.color.blue));
                }
            }
        }


    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.confirm:
                if (carBean!=null){
                    editCar();
                }else {
                    addCar();
                }
                break;
            case R.id.car_brand_lay:
                Intent intent=new Intent(this,CarBrandActivity.class);
                startActivityForResult(intent,1);

                break;
            case R.id.car_model_lay:
                if (brandBean==null){
                    showToast("请先选择车辆品牌");
                    return;
                }
                Intent modelIntent=new Intent(this,CarModelActivity.class);
                modelIntent.putExtra("CAR_BRAND",brandBean);
                startActivityForResult(modelIntent,2);
                break;
            case R.id.car_config_lay:
                if (brandBean==null){
                    showToast("请先选择车辆品牌");
                    return;
                }
                if (modelBean==null){
                    showToast("请先选择车辆型号");
                    return;
                }
                Intent configIntent=new Intent(this,CarConfigActivity.class);
                configIntent.putExtra("CAR_BRAND",brandBean);
                configIntent.putExtra("CAR_MODEL",modelBean);
                startActivityForResult(configIntent,3);
                break;
            case R.id.buy_time_lay:
                TimePickerView buyTime;
                buyTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
                //控制时间范围
                Calendar buyCalendar = Calendar.getInstance();
                buyTime.setRange(1900, buyCalendar.get(Calendar.YEAR));

                if (buyTimeTv.getText().equals("请选择")) {
                    buyTime.setTime(new Date());
                } else {
                    buyTime.setTime(CommonUtil.Date2Millis(buyTimeTv.getText().toString()));
                }
                buyTime.setCyclic(true);
                buyTime.setCancelable(true);
                //时间选择后回调
                buyTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

                    @Override
                    public void onTimeSelect(Date date) {
                        buyTimeTv.setText(CommonUtil.DateFormat(date));
                        buyTimeTv.setTextColor(getResources().getColor(R.color.blue));
                    }
                });
                buyTime.show();
                break;
            case R.id.repay_time_lay:
                TimePickerView repayTime;
                repayTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
                //控制时间范围
                Calendar repayCalendar = Calendar.getInstance();
                repayTime.setRange(1900, repayCalendar.get(Calendar.YEAR));

                if (repayTimeTv.getText().equals("请选择")) {
                    repayTime.setTime(new Date());
                } else {
                    repayTime.setTime(CommonUtil.Date2Millis(repayTimeTv.getText().toString()));
                }
                repayTime.setCyclic(true);
                repayTime.setCancelable(true);
                //时间选择后回调
                repayTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

                    @Override
                    public void onTimeSelect(Date date) {
                        repayTimeTv.setText(CommonUtil.DateFormat(date));
                        repayTimeTv.setTextColor(getResources().getColor(R.color.blue));
                    }
                });
                repayTime.show();
                break;
        }
    }
    @Override
    public void initData() {
        if(carBean !=null){
            setData();
        }
    }
}
